<?php
class Basic {

	function getModel() {
		if (empty ($_GET['model'])) {
			$_GET['model'] = 'default';
		} else {
			$_GET['model'] = strtolower($_GET['model']);
		}
		return $_GET['model'];
	}

	function getAction() {
		if (empty ($_GET['action'])) {
			$_GET['action'] = 'index';
		} else {
			$_GET['action'] = strtolower($_GET['action']);
		}
		return $_GET['action'];
	}

	function initApp() {

	}

	function setModelFile($file) {
		global $tpl, $gModel;
		$vFile = $gModel . "_$file";
		$tpl->set_file($vFile);
		$tpl->n();
	}


	function flash ($message, $url = null,$timeout=0) {
		$_SESSION['flash'][] = $message;
		if($timeout>0){
			$_SESSION['timeout'] = $timeout*1000;
		}
		if($url) {
			if(headers_sent()) {
				echo '<script language="javascript">	document.location.href= "'.$url.'";  </script>';
			} else {
				header('location: '.$url);
			}
			die;
		}
	}

	/*
		替换提交内的明感字符
		replace unsafe characaters
		用法: Post_Get_Fix(替换内容)
		example:Post_Get_Fix(content)
		,"/","&#47;"
	*/
	function Post_Get_Fix($msg) {
		$array1 = array (
			'&',
			'\"',
			"\'",
			'"',
			"'",
			"<",
			">",
			"\\"
		);
		$array2 = array (
			'&amp;',
			'&quot;',
			'&#39;',
			'&quot;',
			'&#39;',
			"&lt;",
			"&gt;",
			"&#92;"
		);
		$msg = str_replace($array1, $array2, $msg);
		return $msg;
	}

	//msg表示需要还原过滤的内容,br为1表示是对内容换行
	//example:msg represents the content that needs revert to original status,br for 1 represents newline.
	//,'&#47;',"/"

	function HTML_Fix($msg, $br = 1) {
		$array1 = array (
			'&#92;',
			'&amp;',
			'&quot;',
			'&#39;',
			"&lt;",
			"&gt;"
		);
		$array2 = array (
			"\\",
			'&',
			'"',
			"'",
			"<",
			">"
		);
		$msg = str_replace($array1, $array2, $msg);
		$msg = ($br == 1) ? nl2br($msg) : $msg;
		return str_replace("><br />", ">", $msg);
	}
	
	
	//send emails
	function mails($mailto='',$title='',$text=''){
		global $mailserver,$mailname,$mailuser,$mailpass;
		include_once"mail.php";
		//check
		if(trim($mailto)!=''){
			$smtp = new smtp($mailserver,25,false,$mailuser,$mailpass);//这里面的?个true是表示使用身份验?,否则不使用身份验?.
			//$smtp->debug = TRUE;//是否显示发?的调试信息
			//$smtp->debug = TRUE;//get feedback
			$smtp->sendmail($mailto, $mailuser, basic::HTML_Fix($mailname), $title, $text,"HTML");
		}
	}
	
	/**
	*  获取当前网站地址
	*/
	function geturl(){
		global $HTTP_SERVER_VARS;
		return 'http://'.$HTTP_SERVER_VARS['HTTP_HOST'].$HTTP_SERVER_VARS['SCRIPT_NAME'].'?'.$HTTP_SERVER_VARS['QUERY_STRING'];
	}

}


/*分页
/*pagination
	num			最大分页数
	perpage		每页多少信息数
	curr_page	当前页数
	pages		分页数量
	mpurl		翻页地址 如果要在翻页ID后加地址比如a.php?action&5&type代码为:a.php?action!!&type
	style		显示风格
*/
function multi($num, $perpage, $curr_page, $pages, $style='',$mpurl='') {
  global $_SERVER,$target;
  if($mpurl==''){
  	 $mpurl = preg_replace("/&page=[0-9]+/is","",basic::geturl());
  }
  
  $mpurl  = explode("!!",$mpurl);
  $mpurl1 = $mpurl[0];
  $mpurl2 = $mpurl[1];
  $mpurl3 = ($mpurl2=='')?'':"+'$mpurl2'";
  
  $multipage = '';
	if($num > $perpage) {
		$page = 5;
		$offset = 2;

		$from = $curr_page - $offset;
		$to = $curr_page + $page - $offset - 1;
		if($page > $pages) {
			$from = 1;
			$to = $pages;
		} else {
			if($from < 1) {
				$to = $curr_page + 1 - $from;
				$from = 1;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$to = $page;
				}
			} else if($to > $pages) {
				$from = $curr_page - $pages + $to;
				$to = $pages;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$from = $pages - $page + 1;
				}
			}
		}
		
		switch($style){
		
		  
		  
		//样式
		//Page 1 of 2 | Items 1 - 10 首 页 上一页 下一页 末 页
	 	  default:
		    if(($curr_page-1)<=1){$pt= 1;}else{$pt=$curr_page-1;}
		    if(($curr_page+1)>=$pages){$pd=$pages;}else{$pd=$curr_page+1;}
		  	$multipage .= "<b>页数:$pages, 查看:";
		  	
		  	if($curr_page=="1"){
		  	   $multipage.=" 1 - $perpage";
		  	}elseif($curr_page==$pages){
		  	   $multipage.=(($curr_page-1)*$perpage+1)." - ".$num;
		  	}else{
		  	   $multipage.= (($curr_page-1)*$perpage+1)." - ".$curr_page*$perpage;
		  	}
		  	$multipage .= "<b>, 转到:";
			
			//js 连接
			$mpurls = preg_replace('/(.+)\/([a-z0-9A-Z]+)\.php/is','\\2.php',$mpurl1);
			
		  	$multipage.="</b> <a onclick=\"link('$mpurls&page=1$mpurl2');return false;\" href=\"$mpurl1&page=1$mpurl2\" style=\"line-height: normal\"><font color='#435a83'>首页</font></a>&nbsp;
			<a onclick=\"link('$mpurls&page=$pt$mpurl2');return false;\" href=\"$mpurl1&page=$pt$mpurl2\" style=\"line-height: normal\"><font color='#435a83'>上一页</font></a>&nbsp;
			<a onclick=\"link('$mpurls&page=$pd$mpurl2');return false;\" href=\"$mpurl1&page=$pd$mpurl2\" style=\"line-height: normal\"><font color='#435a83'>下一页</font></a>&nbsp;
			<a onclick=\"link('$mpurls&page=$pages$mpurl2');return false;\" href=\"$mpurl1&page=$pages$mpurl2\" style=\"line-height: normal\"><font color='#435a83'>末页</font></a>";
			break;
		}
	}else{
	//	$multipage .= '<b>multipage: </b>only one page';
	}
	
	//检测是否需要连接控制
	if(trim($target)!=''){
		$multipage  = str_replace("<a ","<a target='".$target."' ",$multipage);
	}
  return $multipage;
}


//排序处理
function order($ary=array()){
	global $db;
	
	//取得同类别的所有板块
	$querys = $db->query("SELECT * FROM $ary[table] WHERE $ary[where] $ary[order]");
	$i = 0;
	while($fs = $db->fetch_array($querys)){
		$i++;
		if($fs[$ary['idname']]==$ary[id]){
			$ids[$i] = array(
				'id'=>$fs[$ary['idname']],
				"order"=>$i,
			);
			$this_id = $i;//用于判断ID
		}else{
			$ids[$i] = array(
				'id'=>$fs[$ary['idname']],
				"order"=>$i,
			);
		}
	}
	
	//上下规则
	$upnum = ($ary[modes]=='up')?($this_id-1):($this_id+1);
	
	//处理排序
	if(is_array($ids)){
		foreach($ids AS $k=>$v){
			//当前版块的前一个版块
			if($k==$upnum){
				$upid = $this_id;
				$sql = "UPDATE $ary[table] SET $ary[name]='$upid' WHERE ".$ary['idname']."='$v[id]' AND $ary[where]";
				$db->query($sql);
			}else{
				//当前版块
				if($k==$this_id){
					$upid = $upnum;
					$sql = "UPDATE $ary[table] SET $ary[name]='$upid' WHERE ".$ary['idname']."='$v[id]' AND $ary[where]";
					$db->query($sql);
				}else{
					//其他版块
					$upid = $k;
					$sql = "UPDATE $ary[table] SET $ary[name]='$upid' WHERE ".$ary['idname']."='$v[id]' AND $ary[where]";
					$db->query($sql);
				}
			}
		}
	}
}
?>